/**
 * spritesheet动画模板数据
*/
class SheetTemplet{
	private _sheet:egret.SpriteSheet;
	private _actionMap:Object;
	private _actionKeys:Array<string>;
	private _frames:Array<string>;
	private _numReg:RegExp;
	private _autoFrames:boolean;
	public constructor(sheet:egret.SpriteSheet=null,autoFrames:boolean=true) {		
		this._numReg = /\d+/;
		this._autoFrames = autoFrames;
		this.sheet = sheet;
	}
	
	/**
	 * 根据配置自动生成动作模板
	 */
	protected autoFrames():void
	{		
		this.reset();
		let dKey:string;
		let actions:Array<string>;
		for(var key in this._sheet._textureMap)
		{
			let retAry:RegExpExecArray = this._numReg.exec(key);
			dKey = key;
			if(retAry)
			{
				dKey = key.replace(retAry[0],"");//把类似 stand1 这样的帧键转换成stand 标记为一种动作类型
			}
			actions = this._actionMap[dKey];
			if(!actions)
			{
				actions = new Array<string>();
				this._actionMap[dKey] = actions;
			}
			actions.push(key); // 把类似 stand1 stand2 stand3 ...这些帧键储存到数组
			if(this._actionKeys.indexOf(dKey)==-1)
			{
				this._actionKeys.push(dKey); //记录所有的动作类型
			}
			this._frames.push(key); //记录所有帧
		}	
		//this._frames = new Array<string>("attack6", "attack5", "attack2", "attack3", "attack4", "attack1", "attack0");
		//this._frames.sort(this.compareFrames);
	}
	private compareFrames(a:string,b:string):number
	{		
		let numReg:RegExp = /\d+/;
		let retAryA:RegExpExecArray = numReg.exec(a);
		let retAryB:RegExpExecArray = numReg.exec(b);

		if(retAryA && retAryB && (a>b || b>a))
		{
			let nA:number = parseInt(retAryA[0]);
			let nB:number = parseInt(retAryB[0]);
			if(nA<nB)
			{
				return -1;
			}else if(nA>nB)
			{
				return 1;
			}
		}else if(!retAryA && retAryB)
		{
			return -1;
		}else if(retAryA && !retAryB)
		{
			return 1;
		}
		return 0;
	}
	/**
	 * 所有动作的帧
	*/
	public get frames():Array<string>
	{
		return this._frames;
	}

	public set sheet(value:egret.SpriteSheet){
		if(this._sheet!=value)
		{
			this._sheet = value;
			if(this._autoFrames)
			{
				this.autoFrames();
			}

		}
	}
	public get sheet():egret.SpriteSheet
	{
		return this._sheet;
	}

	public reset():void
	{
		this._actionMap = {};
		this._actionKeys = new Array<string>();
		this._frames = new Array<string>();
	}

	/**
	 * 创建动作动画模板
	 * @param action 动作的名称
	 * @param start 开始下标
	 * @param end 结束下标 (包含该下标)
	 * @example createFrames('stand',0,2);得到一个数组 ['stand0','stand1','stand2']
	 * 
	*/
	public createFrames(action:string,start:number,end:number):void
	{
		if(!this._actionMap)
		{
			this.reset();
		}
		let actions:Array<string> = new Array<string>();
		this._actionMap[action] = actions;
		let frame:string;
		for(let i:number=start;i<=end;i++)
		{
			frame = action+i;
			actions.push(frame);
			this._frames.push(frame);
		}
	}
	/**
	 * 获取指定动作的帧集合
	 * 
	*/
	public getActions(actionKey:string):Array<string>
	{
		let ary:Array<string> = this._actionMap[actionKey];
		//ary.sort(this.compareFrames);
		return ary;
	}
	/**
	 * 获取指定动作的某一范围内的动画帧
	 * @param action 要播放哪个动作的动画(默认空串表示播放所有动作)
	 * @param start 开始帧(默认0表示第一帧)
	 * @param end 结束帧(默认-1 表示最后一帧)	 
	*/
	public getActionFrames(actionKey:string,start:number,end:number):Array<string>
	{
		let frames:Array<string>;
		if(actionKey!='')
		{
			frames = this.getActions(actionKey);
		}else
		{
			frames =this._frames;
		}
		end<0?end=frames.length:null;
		return frames.slice(start,end);
	}
	/**
	 * 获取纹理
	*/
	public getTexture(frameKey:string):egret.Texture
	{
		return this._sheet.getTexture(frameKey);
	}

}